package club.yunqiang.rabbitmqapi.returnlistener;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 张云强
 * @date 2019/12/7
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setHost("localhost");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        String exchange = "test_return_exchange";
        String routingKey = "return.save";
        String routingKeyError = "abc.save";

        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.err.println("----- handle return -----");
                System.err.println("replyCode：" + replyCode);
                System.err.println("replyText：" + replyText);
                System.err.println("exchange：" + exchange);
                System.err.println("routingKey：" + routingKey);
                System.err.println("properties：" + properties);
                System.err.println("body：" + new String(body));
            }
        });

        String msg = "Hello Rabbit MQ Return Message";
        channel.basicPublish(exchange, routingKeyError, true, null, msg.getBytes());
    }

}
